import { login } from '../../api/user'
import storage from '../../utils/storage'

const state = {
  userInfo: storage.getItem('userInfo'),
  token: storage.getItem('token')
}

const mutations = {
  SET_USER_INFO (state, paylfin) {
    state.userInfo = paylfin.message
  },
  SET_TOKEN (state, paylfin) {
    state.token = paylfin.message.token
  }
}

const actions = {
  login ({ commit }, paylfin) {
    return new Promise((resolve, reject) => {
      login(paylfin).then(res => {
        commit('SET_USER_INFO', res.data)
        commit('SET_TOKEN', res.data)
        storage.setItem('userInfo', res.data.message)
        storage.setItem('token', res.data.message.token)
        resolve()
      }).catch(err => {
        reject(err)
      })
    })
  }
}

const getters = {

}

export default {
  namespaced: true,
  state,
  mutations,
  actions,
  getters
}
